<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>简单动画函数封装</title>
  <style>
    div {
      position: absolute;
      left: 0;
      width: 100px;
      height: 100px;
      background-color: pink;
    }

    span {
      position: absolute;
      left: 0;
      top: 200px;
      /*display: block;*/
      width: 150px;
      height: 150px;
      background-color: purple;
      color: #ff7c2d;
    }
  </style>
</head>
<body>
  <div></div>
  <span>夏雨荷</span>
  <script>
    // 简单动画函数封装obj目标对象 target 目标位置
    function animate (obj, target) {
      var timer = setInterval(function () {
        if (obj.offsetLeft >= target) {
          // 停止动画 本质是停止定时器
          clearInterval(timer)
        } else {
          obj.style.left = obj.offsetLeft + 1 + 'px'
          // obj.style.position = 'absolute'    可以将定位也封装到函数里
        }
      }, 30)
    }

    var div = document.querySelector('div')
    var span = document.querySelector('span')
    // 调用函数
    animate(div, 300)
    animate(span, 200)
  </script>
</body>
</html>